<?
$isReloadable = false;
$isPopUp = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sql = "SELECT d.*, v.chiave, v.c01, v.c02, v.c03, v.c04, v.c05, v.c06, v.c07, v.c08, v.c09".
			", v.c10, v.c11, v.c12 ".
			" FROM ovf_tabelle d ".
			" LEFT JOIN ovf_tabelle_val v ON v.id_tabella = d.id_tabella ".
			" WHERE d.id_tabella = ".intval(reqVar('id')).
			" ORDER BY v.chiave";
$objDB->query($sql);
$rec = $objDB->fetch();
$nomeFileExp = "valdom_".$rec['cod_tabella'].".csv";
$aCampi = explode(',',$rec['lista_campi']);
$sFile = $sep = "";
foreach($aCampi as $kX => $nX) {
	$sFile .= $sep.'"'.$nX.'"';
	$sep = ';';
}
$sFile .= "\r\n";
while($rec) {
	$sep = '';
	foreach($aCampi as $kX => $nX) {
		$cXX = ($kX == 0) ? "chiave" : 'c'.str_pad($kX,2,'0',STR_PAD_LEFT);
      $sFile .= $sep.'"'.str_replace("\r","",str_replace("\n"," ",str_replace('"',"'",$rec[$cXX]))).'"';
      $sep = ';';
   }
   $sFile .= "\r\n";
   $rec = $objDB->fetch();
}

headerDownLoad(strlen($sFile),$nomeFileExp);
echo $sFile;
exit();
